package com.zm.demo.spring.tx.main;


import com.zm.demo.spring.tx.manager.UserManager;
import com.zm.demo.spring.tx.mapper.UserMapper;
import com.zm.demo.spring.tx.po.User;
import com.zm.demo.spring.tx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserManager userManager;

    @Autowired
    UserService userService;

    @RequestMapping("/users")
    public List<User> getUsers(){
        return userService.getUsers();
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Integer addUser(@RequestBody User user){
        return userManager.insertUser(user);
    }

    @RequestMapping("/update")
    public Integer update(){
        User user1 = new User();
        user1.setId(1L);
        user1.setName(String.valueOf(new Random().nextInt()));
        return userService.updateById(user1);
    }

    @RequestMapping("/updateBatch")
    public Integer updateBatch(){
        List<User> users = new ArrayList<>();
        for (Integer i = 0; i < 10 ; i++) {
            User user1 = new User();
            user1.setId(i.longValue());
            user1.setName(String.valueOf(new Random().nextInt()));
            users.add(user1);
        }
        return userService.updateBatch(users);
    }

    @RequestMapping("/updateBatch1")
    public Integer updateBatch1(){
        List<User> users = new ArrayList<>();
        for (Integer i = 0; i < 10 ; i++) {
            User user1 = new User();
            user1.setId(i.longValue());
            user1.setName(String.valueOf(new Random().nextInt()));
            users.add(user1);
        }
        return userService.updateBatch1(users);
    }


}
